package github.kawaiior.yuanchuxiuzhen.client.gui.panel;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import github.kawaiior.yuanchuxiuzhen.util.MathUtil;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.common.util.LazyOptional;

public class PanelGui extends Screen {

    ResourceLocation YCXZ_PANEL_GUI_TEXTURE = new ResourceLocation(Yuanchuxiuzhen.MODID, "textures/gui/kgui.png");

    protected PanelGui(ITextComponent titleIn) {
        super(titleIn);
    }

    @Override
    protected void init() {
        assert this.minecraft != null;
        this.minecraft.keyboardListener.enableRepeatEvents(true);
        super.init();
    }

    public void renderFontHandle(MatrixStack matrixStack, FontRenderer fontRenderer, ManaPower power){
        // 保存当前的OpenGL状态
        matrixStack.push();
        // 使内容缩放0.7
        float scale = 0.7F;
        matrixStack.scale(scale, scale, scale);

        // 规定字体颜色
        int color = 0xFFFFFF;

        // 绘制左侧属性数值
        int left_x = (int)((width / scale)/2F - 104);
        fontRenderer.drawString(matrixStack, MathUtil.formatFloat(power.getMaxHealth(), 1), left_x, 50, color);
        fontRenderer.drawString(matrixStack, MathUtil.formatFloat(power.getMaxMana(), 1), left_x, 73, color);
        fontRenderer.drawString(matrixStack, MathUtil.formatFloat(power.getAttack(), 1), left_x, 96, color);
        fontRenderer.drawString(matrixStack, MathUtil.formatFloat(power.getDefense(), 1), left_x, 119, color);
        fontRenderer.drawString(matrixStack, MathUtil.formatFloat(power.getManaRecovery(), 1), left_x, 142, color);
        fontRenderer.drawString(matrixStack, MathUtil.formatFloat(power.getMerit(), 1), left_x, 165, color);
        fontRenderer.drawString(matrixStack, String.valueOf(power.getThunderTribulation()), left_x, 188, color);
        fontRenderer.drawString(matrixStack, String.valueOf(power.getEvilSpirit()), left_x, 211, color);

        // 绘制中间属性数值
        int middle_x = (int)((width / scale)/2F + 10);
        fontRenderer.drawString(matrixStack, String.valueOf(power.getRefining()), middle_x, 27, color);
        fontRenderer.drawString(matrixStack, String.valueOf(power.getAlchemy()), middle_x, 50, color);
        fontRenderer.drawString(matrixStack, String.valueOf(power.getCharacter()), middle_x, 73, color);
        fontRenderer.drawString(matrixStack, String.valueOf(power.getFragment()), middle_x, 96, color);

        // 还原OpenGL状态
        matrixStack.pop();
    }

    public void renderFont(MatrixStack matrixStack){
        assert this.minecraft != null;
        FontRenderer fontRenderer = this.minecraft.fontRenderer;
        PlayerEntity player = this.minecraft.player;
        LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power)->{
            renderFontHandle(matrixStack, fontRenderer, power);
        });

    }

    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        this.renderBackground(matrixStack);
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        assert this.minecraft != null;
        this.minecraft.getTextureManager().bindTexture(YCXZ_PANEL_GUI_TEXTURE);
        int textureWidth = 256;
        int textureHeight = 256;
        // 渲染贴图
        this.blit(matrixStack, (width - textureWidth)/2, 10, 0, 0, 256, 256, textureWidth, textureHeight);
        // 渲染属性数值
        renderFont(matrixStack);
        super.render(matrixStack, mouseX, mouseY, partialTicks);
    }

}
